package com.superman.supermarket.client.view.refundapplication;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.supermarket.client.constant.RefundTypeConstants;
import com.superman.supermarket.client.widget.bottombutton.BottomButton;
import com.superman.supermarket.client.widget.header.CommonPageHeader;
import com.superman.supermarket.client.widget.input.type.CanEmptyInput;
import com.superman.supermarket.client.widget.input.type.NumberInput;
import com.superman.supermarket.client.widget.listbox.MyListBox;

/**
 * 退货退款申请
 * 
 * @author superman
 * @version 2018年8月18日下午10:46:18
 */
public class RefundApplicationViewImpl extends Composite implements RefundApplicationView {

	interface RefundApplicationViewImplUiBinder extends UiBinder<Widget, RefundApplicationViewImpl> {
	}

	private static RefundApplicationViewImplUiBinder uiBinder = GWT.create(RefundApplicationViewImplUiBinder.class);

	@UiField
	protected CanEmptyInput memoinput;
	@UiField
	protected CommonPageHeader pageheader;
	protected RefundApplicationViewPresenter presenter;
	@UiField
	protected HTML pricedeschtml;
	@UiField
	protected NumberInput priceinput;
	private double refundablePrice;
	@UiField
	protected Image skuimg;
	@UiField
	protected HTML skuinfohtml;
	@UiField
	protected FlowPanel skupanel;

	@UiField
	protected BottomButton submitbtn;
	@UiField
	protected MyListBox typelist, reasonlist;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月18日 下午10:46:21
	 */
	public RefundApplicationViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		priceinput.getInputbase().getElement().getStyle().setColor("red");

	}

	protected Map<String, Object> getApplicationData() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REFUND_TYPE", Integer.parseInt(typelist.getValue()));
		map.put("REFUND_FEE", Double.parseDouble(priceinput.getValue()));
		map.put("REASON", reasonlist.getSelectedItem());
		if (!StringUtils.isEmpty(memoinput.getValue()))
			map.put("MEMO", memoinput.getValue());
		return map;
	}

	@Override
	public void init() {
		submitbtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				if (priceinput.valid()) {
					double refundFee = Double.parseDouble(priceinput.getValue());
					if (refundFee == 0) {
						XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("退款金额必须大于0"));
					} else if (refundFee > refundablePrice) {
						XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("退款金额超出最大可退款金额"));
						priceinput.setValue(refundablePrice + "");
					} else {
						presenter.submitApplication(getApplicationData());
					}
				}

			}
		});
	}

	@Override
	public void setPageHeaderText(String text) {
		pageheader.setText(text);
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (RefundApplicationViewPresenter) presenter;
	}

	@Override
	public void setReasonList(List<String> reasonList) {
		for (int i = 0; i < reasonList.size(); i++) {
			reasonlist.addItem(reasonList.get(i), i + "");
		}
		reasonlist.setDefaultText();
	}

	@Override
	public void setRefundSkuInfo(Map<String, Object> skuMap, boolean onlyRefundMoney) {
		typelist.addItem("仅退款", RefundTypeConstants.REFUND_ONLY_MONEY + "");
		if (!onlyRefundMoney) {
			typelist.addItem("退货退款", RefundTypeConstants.REFUND_MONEY_AND_RETUEN + "");
		}
		typelist.setDefaultText();

		skuimg.setUrl(String.valueOf(skuMap.get("SKU_PIC")));
		skuinfohtml.setHTML(String.valueOf(skuMap.get("SKU_NAME")) + "&nbsp;&nbsp;" + String.valueOf(skuMap.get("SKU_PROPS_NAME")));

		refundablePrice = Double.parseDouble(String.valueOf(skuMap.get("REFUNDABLE_FEE")));
		priceinput.setValue(refundablePrice + "");
		priceinput.setReadOnly(onlyRefundMoney);
		pricedeschtml.setHTML("最多:¥&nbsp;" + refundablePrice);
	}

}
